package com.welink.cmp.meatdata.command;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welink.cmp.dao.metadata.RegionDaoService;
import com.welink.cmp.dao.metadata.dataobject.RegionDO;
import com.welink.cmp.dto.metadata.region.RegionAddCmd;
import com.welink.cmp.dto.metadata.region.RegionCO;
import com.welink.cmp.dto.metadata.region.RegionPageQry;
import com.welink.cmp.dto.metadata.region.RegionUpdCmd;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class RegionCmdExe {

    @Resource
    private RegionDaoService regionDaoService;

    public Long save(RegionAddCmd addCmd) {
        RegionDO region = new RegionDO();
        BeanUtils.copyProperties(addCmd, region);
        if (addCmd.isNeedsOperator()){
            region.setCreateBy(addCmd.getOperator());
        }
        regionDaoService.save(region);
        return region.getId();
    }

    public Boolean update(RegionUpdCmd updCmd) {
        RegionDO region = new RegionDO();
        BeanUtils.copyProperties(updCmd, region);
        if (updCmd.isNeedsOperator()){
            region.setUpdateBy(updCmd.getOperator());
        }
        return regionDaoService.updateById(region);
    }

    public RegionCO queryById(Long id) {
        RegionDO region = regionDaoService.getById(id);
        RegionCO regionCO = new RegionCO();
        BeanUtils.copyProperties(region, regionCO);
        return regionCO;
    }

    public Page<RegionDO> pageQuery(RegionPageQry pageQry) {
        return regionDaoService.pageQry(pageQry);
    }

    public List<RegionDO> getAllRegion() {

        final List<RegionDO> list = regionDaoService.list();
        return list;
    }
}